package com.moinut.studentattendancemanager.view.student;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import com.moinut.studentattendancemanager.controller.TeacherController;
import com.moinut.studentattendancemanager.controller.callback.OnGetListListener;
import com.moinut.studentattendancemanager.model.vo.TeacherInfo;
import com.moinut.studentattendancemanager.utils.WindowJumper;
import com.moinut.studentattendancemanager.view.BaseFrame;

/**
 * @author MOILING
 */
public class GetTeacherFrame extends BaseFrame<GetTeacherFrame.GetTeacherPanel> {
    private static final long serialVersionUID = 1L;

    private static final int DEFAULT_WIDTH = 500;
    private static final int DEFAULT_HEIGHT = 500;

    public GetTeacherFrame() {
        super("查看教师信息", DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    /* package */ class GetTeacherPanel extends JPanel implements ActionListener {
        private static final long serialVersionUID = 1L;

        private JTable mResultTable;
        private JScrollPane mResultScrollPane;
        private JButton mBackButton;
        private JPanel mResultPanel, mButtonPanel;
        private DefaultTableModel mTableModel;

        /*package*/ GetTeacherPanel() {
            setLayout(new BorderLayout());
            initResultPanel();
            initButtonPanel();
            initData();
        }

        private void clearTable() {
            for (int i = mTableModel.getRowCount() - 1; i >= 0; i--) {
                mTableModel.removeRow(i);
            }
        }

        private void initData() {
            TeacherController.getAllTeachersInfo(new OnGetListListener<TeacherInfo>() {
                @Override
                public void onSuccess(List<TeacherInfo> teachersInfo) {
                    clearTable();
                    for (TeacherInfo teacherInfo : teachersInfo) {
                        mTableModel.addRow(new Object[]{
                                teacherInfo.getTeacherName(),
                                teacherInfo.getCourseName(),
                                teacherInfo.getCollegeName()
                        });
                    }
                }

                @Override
                public void onError(String error) {
                    clearTable();
                    JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                }
            });
        }

        private void initButtonPanel() {
            mBackButton = new JButton("返回上级菜单");
            mButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mButtonPanel.add(mBackButton);
            add(mButtonPanel, BorderLayout.SOUTH);

            mBackButton.addActionListener(this);
        }

        private void initResultPanel() {
            String[] columnName = {"姓名", "任课课程", "所属学院"};
            mTableModel = new DefaultTableModel(null, columnName);
            mResultTable = new JTable(mTableModel);
            mResultScrollPane = new JScrollPane(mResultTable,
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

            mResultPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mResultPanel.add(mResultScrollPane);
            add(mResultPanel, BorderLayout.CENTER);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(mBackButton)) {
                WindowJumper.back(GetTeacherFrame.this);
            }
        }
    }

    @Override
    protected GetTeacherPanel onSetContentPane() {
        return new GetTeacherPanel();
    }
}
